<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
$(document).ready(function(){
	$("#putDate").datepicker({dateFormat:"dd/mm/yy"});
	get_list_user_room('',1);
	query(1);		
	$("#btnSearch").click(function(){
		get_list_user_room('', 1);
	});
	//Code bat phim enter
	$("#txtName").keypress(function (e){
	  	var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (keycode == 13){
			get_list_user_room('', 1);
			return false;
		}else{return true;}
	});

	$("#slRoom").change(function(){
		get_list_user_room('', 1);
	});

	$('#tblUserRoom tr:nth-child(even)').addClass('alt');
	$('#tblUserRoom tr:nth-child(odd)').addClass('alt2');
	$('.alt').css('background-color','#FFFFFF');
	$('.alt2').css('background-color','#EEEEEE');
});

function GoEndPage(){
	var total = $('#total_row').val();
	var per_page=$('#per_page').val();
	var endpage= parseInt($('#total_row').val()/$('#per_page').val());
	if( total%per_page!=0)
		endpage= endpage+1;
	//alert(endpage);
    query(endpage);
    
}
//Lay danh sach phong
function query(page){
	var room_id=$('#search_roomid').val();
	var room_name=$('#search_roomname').val();
	var per_page=$('#per_page').val();
	var path='room_id='+room_id+'&room_name='+room_name+'&page='+page+'&per_page='+per_page;
    $.ajax({
      	type:"POST",
      	url: "data/room/book_room/total_page.php",
      	data: path,
        async: true,
    	success: function(data) {
		    //alert(data);
    		$("#totalPage").html(data);
            $('#total_row').val(data);
			load_listpage(page);
			//			
		}
	});
	
}

//
function load_listpage(page){
       	    var room_id=$('#search_roomid').val();
			var room_name=$('#search_roomname').val();
			var per_page=$('#per_page').val();
			var path='room_id='+room_id+'&room_name='+room_name+'&page='+page+'&per_page='+per_page;
					//alert(path);
            $.ajax({
      			type:"POST",
      			url: "data/room/book_room/list_page.php",
      			data: path,
                async: true,
    			success: function(data) {
                    var total=$('#total_row').val();
                    if(total/per_page>1){
    				    $("#listpage_data").html(data);
                        $('#listpage').show();
                    }else {
                        $("#listpage_data").html("");
                        $('#listpage').hide();                        
                    }
                    $.ajax({
        				type:"POST",
        				url: "data/room/book_room/list_room.php",
        				data: path,
        				async: true,
        				success: function(data) {  
        					$('#TabContentDiv').html(data);
        					// $('#dsRoom').tableFilter(true);
        				}
        			}); 
			     }
		     }); 
    }


function open_user_room(id){
	$("#divSearchUserRoom").show();
	$("#divRoomID").hide();
	get_list_user_room(id, 1);
}
function GoEndPage2(){
	var total = $('#total-row-user-room').val();
	var per_page=$('#per_page_user_room').val();
	var endpage= parseInt($('#total-row-user-room').val()/$('#per_page_user_room').val());
	if( total%per_page!=0)
		endpage= endpage+1;
	//alert(endpage);
	get_list_user_room('', endpage);    
}
//
function get_list_user_room(id, page){
	var room_id= $("#slRoom").val();
	var uname=$("#txtName").val();
	var date = $("#putDate").val();
	var per_page=$('#per_page_user_room').val();	
	if(id==''){
		room_id = $("#slRoom").val();
	}else{
		room_id = id;
		$("#slRoom").val(room_id);
	}
	var path='room_id='+room_id+'&uname='+uname+'&date='+date+'&page='+page+'&per_page='+per_page;
    $.ajax({
      	type:"POST",
      	url: "data/room/book_room/total_user_room_page.php",
      	data: path,
        async: false,
    	success: function(data) {
    		$("#total-user-room").html(data);
            $("#total-row-user-room").val(data);
            //Load phan trang
            load_listpage_user_room(page);
        	$.ajax({
        		type:"POST",
        		url: "data/room/book_room/list_user_room.php",
        		data: path,
        		async: false,
        		success: function(data) {   	
        		    $("#divContent").html(data);
        	 	}
        	}); 
		}
	});	
}

function load_listpage_user_room(page){
    var room_id= $("#slRoom").val();
	var uname=$("#txtName").val();
	var per_page=$('#per_page_user_room').val();
	var date = $("#putDate").val();
	var path='room_id='+room_id+'&uname='+uname+'&date='+date+'&page='+page+'&per_page='+per_page;
	
	$.ajax({
		type:"POST",
		url: "data/room/book_room/list_user_room_page.php",
		data: path,
	    async: false,
		success: function(data) {
	        var total=$('#total-row-user-room').val();
	        if(total/per_page>1){
			    $("#listpage_data_user_room").html(data);
	            $('#listpage').show();
	        }else {
	            $("#listpage_data_user_room").html("");
	            $('#listpage').hide();	            
	        }
	     }
	 }); 
}

function book_room(room_id){
	var path='room_id='+room_id;
	$.ajax({
		type:"POST",
		url: "data/room/book_room/book_room.php",
		data: path,
	    async: false,
		success: function(data) {
			$("#divRoomID").html(data);
            $("#divSearchUserRoom").hide();
			$("#divRoomID").show();
	     }
	 }); 
}    
//Chuc nang huy dat phong
function cancel_room(id, start_time){
	//28/03/2012 12:30
	year = parseInt(start_time.substring(6, 10));
	month = parseInt(start_time.substring(3, 5)) - 1;
	day = parseInt(start_time.substring(0, 2));
	hours = parseInt(start_time.substring(11, 13));
	if(hours<2){
		hours = 24 - 1;
		day = day -1;
	}else{
		hours = hours - 2;
	}
	
	minutes = parseInt(start_time.substring(14, 16));
	date_check = new Date(year, month, day, hours, minutes);
	now = new Date();
	xx =date_check.getTime();
	zz = now.getTime();
	if(xx > zz){
		if(!confirm('この予約をキャンセルと罰金は５、０００VNDでかかることになっていますが。本当、キャンセルしますか。')){
			return;
		}
	}else{
		alert('予約時間の2時間前の以前だけキャンセルすることができます！！！');
		return;
	}
	
	//Thuc hien huy dat phong
	var path='order_id='+id;
	$.ajax({
		type:"POST",
		url: "data/room/book_room/cancel_user_room.php",
		data: path,
	    async: false,
		success: function(data) {
			alert(data);
			get_list_user_room('', 1);	
	    }
	 });
}
</script>
</head>
<body>
<div id='tabheader' class="title" style="width: 99.4%">Quản lý phòng -> Quản lý thông tin phòng</div>
<table class="ctable" width="100%">
<tr>
	<td width="30%" style="padding-right: 10px" align="right">室名 ID</td>
	<td width="15%"><input type="text" class="cinput" id="search_roomid"></td>
	<td width="10%" style="padding-right: 10px" align="right">室名</td>
	<td width="15%"><input type="text" class="cinput" id="search_roomname"></td>
	<td width="30%" align="left"><input value='索引する' type="button"
		onClick="query(1)" style="width: 90px" class="cbutton"></td></tr>
</table>
<div id='TabContentDiv' class="tabContent"></div>
<div id="phantrang">
<table class="ctable" width="100%">
	<tr align="center" height="10px;">
		<td align="left" width="20%">総計
		<div id="totalPage" style="display: inline;"></div>
		<input type="hidden" id='total_row'> レコード</td>
		<td width="60%" style="display: inline;">
		<div id="listpage_data"
			style="border-style: 0px; display: inline; clear: both;"></div>
		</td>
		<td align="right" width="40%" style="display: inline;"
			id='record_view' colspan=2>表す &nbsp;<select id="per_page"
			onChange="query(1)">
			<option value="5">5 レコード</option>
		</select></td>
	</tr>
</table>

</div>
<div style="width: 100%;" id="divRoomID" class="cdiv"></div>
<div id="divSearchUserRoom">
<table class="ctable" id="tblSearch" width="100%">
	<tr class="title">
		<td colspan="6" align=left>予約者リスト</td>
	</tr>
	<tr>
		<td align=right width="15%">アカウント名:</td>
		<td align=left width="15%"><input type="text" class="cinput"
			id="txtName" width="70%" /></td>
		<td align=right width="10%">室名:</td>
		<td align=left width="10%"> 				
			<?php 
				include_once 'get_list_room.php';						
			?>				
			</td>
		<td align=right>予約年月日:</td>	
		<td align=left>
			<input type="text" id="putDate" name="putDate" class="cdate" style="width: 30%"></input>&nbsp;&nbsp;
			<input type="button" class="cbutton" value="索引する" name="btnSearch" id="btnSearch"></input></td>
	</tr>
</table>
<br>

<div id="paging-user-room">
<table class="ctable" width="100%">
	<tr align="center" height="10px;">
		<td align="left" width="20%">総計:
		<div id="total-user-room" style="display: inline;"></div> レコード
		<input type="hidden" id="total-row-user-room"
			name="total-row-user-room"></td>
		<td width="60%" style="display: inline;">
		<div id="listpage_data_user_room"
			style="border-style: 0px; display: inline; clear: both;"></div>
		</td>
		<td align="right" width="40%" style="display: inline;"
			id='record_view' colspan=2>表す &nbsp;<select
			id="per_page_user_room" onChange="get_list_user_room('',1)">
			<option value="10">10 レコード</option>
			<option value="20">20 レコード</option>
			<option value="50">50 レコード</option>
		</select></td>
	</tr>
</table>
</div>
<div id="inform-user-room"></div>
<div id="divContent"></div>
</div>

</body>
</html>